package com.collectplus.express.self;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;

import droid.frame.view.ScaledImageView;

/**
 * 智能柜箱子图片浏览
 *
 * @author kaka<br>
 *         2017年3月22日
 */
public class SelfBoxPictrueActivity extends BaseActivity2 {
    /**
     * 图片之源
     */
    private int[] pictrues = new int[]{R.drawable.self_box_enlarge_tiny, R.drawable.self_box_enlarge_small, R.drawable.self_box_enlarge_medium, R.drawable.self_box_enlarge_big};
    private ViewPager mPager;
    private LinearLayout mPointParent;
    /**
     * 选择状态
     */
    private CheckBox mCheckBox;
    /**
     * 选择的柜子
     */
    private int chooseId;
    /**
     * 记录viewpager滑动position
     */
    private int selectedId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        chooseId = getIntent().getIntExtra("checkPosition", 0);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        super.findViewById();
        setContentView(R.layout.self_box_pictrue);
        mPager = (ViewPager) findViewById(R.id.box_viewpager);
        mPointParent = (LinearLayout) findViewById(R.id.selected_point);
        mCheckBox = (CheckBox) findViewById(R.id.check_view);
        mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked && chooseId == selectedId) {
                    mCheckBox.setChecked(true);
                } else if (isChecked && chooseId != selectedId) {
                    mCheckBox.setChecked(false);
                }
            }
        });
        mPager.setOffscreenPageLimit(pictrues.length);
        mPager.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                selectCheckBoxState(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        mPager.setAdapter(new ImageAdapter());

        mPointParent.removeAllViews();
        for (int i = 0; i < pictrues.length; i++) {
            ImageView child = (ImageView) LayoutInflater.from(this).inflate(R.layout.viewpager_dot, mPointParent, false);
            if (i == 0) {
                child.setImageResource(R.drawable.dot_light_2);
            } else {
                child.setImageResource(R.drawable.dot_dark_2);
            }
            mPointParent.addView(child);
        }
        //刚进入SelfBoxPictrueActivity这个类的时候，如果chooseId为0的时候，并且设置了setCurrentItem(chooseId)，不会调用 mPager.addOnPageChangeListener这个方法
        //中onPageSelected()方法，所以在下面进行了判断来单独进行处理
        if (chooseId == 0) {
            selectCheckBoxState(chooseId);
            mPager.setCurrentItem(chooseId);
        } else {
            mPager.setCurrentItem(chooseId);
        }
    }

    private void selectCheckBoxState(int arg0) {
        int childCount = mPointParent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            ImageView pointView = (ImageView) mPointParent.getChildAt(i);
            if (arg0 == i) {
                pointView.setImageResource(R.drawable.dot_light_2);
            } else {
                pointView.setImageResource(R.drawable.dot_dark_2);
            }
        }
        selectedId = arg0;
        if (arg0 == chooseId) {
            mCheckBox.setChecked(true);
            mCheckBox.setText("已选");
        } else {
            mCheckBox.setChecked(false);
            mCheckBox.setText("未选");
        }
    }

    class ImageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pictrues == null ? 0 : pictrues.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            LinearLayout contentView = (LinearLayout) LayoutInflater.from(SelfBoxPictrueActivity.this).inflate(R.layout.self_box_pictrue_item, null);
            ScaledImageView imageView = (ScaledImageView) contentView.findViewById(R.id.box_image);

            imageView.setImageResource(pictrues[position]);
            ((ViewPager) container).addView(contentView, 0);
            return contentView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((LinearLayout) object);
        }
    }
}
